﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Assignment
{
    public partial class Form1 : Form
    {
        private string directoryRoot;
      
        // Blocking queue
        private ConcurrentQueue<IList<string>> blockingQueue = new ConcurrentQueue<IList<string>>();

        // Producer for blocking queue
        FileReader reader;

        // Consumer 
        FileWriter writer;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            button3.Enabled = false;
            button4.Enabled = false;
            button6.Enabled = false;
            button5.Enabled = false;
        }

        private void folderBrowserDialog1_HelpRequest(object sender, EventArgs e)
        {
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                int threads = Int32.Parse(textBox1.Text);

                if (threads <= 0)
                {
                    MessageBox.Show(
                        "Number of threads must be a positive integer",
                        "Invalid number of threads",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Exclamation);
                }
                else
                {
                    if (string.IsNullOrEmpty(directoryRoot))
                    {
                        MessageBox.Show(
                            "The directory to search for files has to be set",
                            "Search directory not set",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Exclamation);
                    }
                    else
                    {
                        reader = new FileReader(blockingQueue, directoryRoot, threads);
                        reader.Read();
                        button1.Enabled = false;
                        button3.Enabled = true;
                        button6.Enabled = true;
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show(
                    "Unable to parse the number of threads field. It must be a positive integer",
                    "Invalid number of threads",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Exclamation);
            }
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox3.Text))
            {
                MessageBox.Show(
                    "Output file name cannot be empty",
                    "Missing parameer",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Exclamation);
            }
            else
            {
                FileWriter writer = new FileWriter(blockingQueue, textBox3.Text);
                writer.Write();

                button4.Enabled = true;
                button5.Enabled = true;
                button2.Enabled = false;
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (button3.Text.Equals("Pause Readers"))
            {
                reader.Stop();
                button3.Text = "Resume Readers";
            }
            else if (button3.Text.Equals("Resume Readers"))
            {
                reader.Resume();
                button3.Text = "Pause Readers";
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                directoryRoot = folderBrowserDialog1.SelectedPath;
            }
        }

        // Abort button click
        private void button6_Click(object sender, EventArgs e)
        {
            button6.Enabled = false;
            button1.Enabled = true;
            button3.Text = "Pause Readers";
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (button4.Text.Equals("Pause Writers"))
            {
                reader.Stop();
                button4.Text = "Resume Writers";
            }
            else if (button4.Text.Equals("Resume Writers"))
            {
                reader.Resume();
                button4.Text = "Pause Writers";
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            button5.Enabled = false;
            button2.Enabled = true;
            button4.Text = "Pause Writers";
        }
    }
}
